﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class FormPackages : Form
    {
        //class vars

        public FormPackages()
        {
            InitializeComponent();
        }


        /// Validation of the input information

        SqlConnection myConn = new SqlConnection("server=(local);database=TravelExperts;integrated security=SSPI");

        private bool validateData()
        {
            //Packages myPackage = new Packages();

            if (double.Parse(txtAgencyComm.Text) > double.Parse(txtPkgPrice.Text))
            {
                MessageBox.Show("Agency commision can not be greater than the base price");
                return false;
            }

            if (dtpPkgStart.Value > dtpPkgEnd.Value)
            {
                MessageBox.Show(" End of Package date can not be later than the start of Package date");
                return false;
            }

            if (txtPkgName.Text == "")
            {
                MessageBox.Show("Package name can not be null");
                return false;
            }

            if (txtPkgDescription.Text == "")
            {
                MessageBox.Show("Description can not be null");
                return false;
            }

            return true;

        }

        ///Get all the information for insert into database

        private Packages PackageDetails()
        {
            Packages myPackage = new Packages();
            if (validateData())
            {
                myPackage.PackageId = int.Parse(txtPkgId.Text);
                myPackage.PkgName = txtPkgName.Text;
                myPackage.PkgStartDate = dtpPkgStart.Value;
                myPackage.PkgEndDate = dtpPkgEnd.Value;
                myPackage.PkgBasePrice = decimal.Parse(txtPkgPrice.Text);
                myPackage.PkgDesc = txtPkgDescription.Text;
                myPackage.PkgAgencyCommission = decimal.Parse(txtAgencyComm.Text);
            }
            return myPackage;
        }
        ////////////////////  end of validation and package details/////////////////////////////
        private void eXITToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        ////////////////////exit application////////////////////////////////////////////////////

        private void PopulateCmb()
        {
            //Added by: Moses
            //Added on: 2012/01/20

            //function that populates the search field comboboxes with values from
            //Products and Suppliers tables

            //clear all comboboxes first
            foreach (Control ctrl in grpProducts.Controls)
            {
                if (ctrl.GetType() == typeof(ComboBox))
                    { ((ComboBox)ctrl).Items.Clear(); }
            }

            //get products and suppliers
            List<Products> products = new DbAccess().SelectProducts();
            List<Suppliers> suppliers = new DbAccess().SelectSuppliers();

            //add an option for "ALL" (no filtering)
            this.cmbSearchProducts.Items.Add("ALL");
            this.cmbSearchSuppliers.Items.Add("ALL");
            
            //add the entries to the combobox
            foreach (Products entry in products)
                { this.cmbSearchProducts.Items.Add(entry); }

            foreach (Suppliers entry in suppliers)
                { this.cmbSearchSuppliers.Items.Add(entry); }

            //populate the selected search parameter
            this.cmbSearchSelected.Items.Add("ALL");
            this.cmbSearchSelected.Items.Add("YES");
            this.cmbSearchSelected.Items.Add("NO");

        }

        private void clearPkgInfoPane()
        {
            //Added by: Moses
            //Added on: 2012/01/20

            foreach (Control ctrl in gpbPackages.Controls)
            {
                //clear all textboxes
                if (ctrl.GetType() == typeof(TextBox))
                    { ctrl.Text = ""; }

                //set all datetimepickers to today's date
                if (ctrl.GetType() == typeof(DateTimePicker))
                    { ((DateTimePicker)ctrl).Value = DateTime.Today; }

                //set all comboboxes selectedindex to 0
                if (ctrl.GetType() == typeof(GroupBox))
                {
                    foreach (Control gpbCtrl in ((GroupBox)ctrl).Controls)
                    {
                        if (gpbCtrl.GetType() == typeof(ComboBox))
                            { ((ComboBox)gpbCtrl).SelectedIndex = 0; }
                    }
                }
            }

            //fill checkedlistbox with all available products from productsSuppliers table
            List<ProductsSuppliers> proSupList = new DbAccess().SelectProductsSuppliers();

            foreach (ProductsSuppliers entry in proSupList)
                { clbProdSups.Items.Add(entry, false); }

        }

        private void clearPkgListPane()
        {
            //Added by: Moses
            //Added on: 2012/01/20
            
            //clear the listbox and populate with all packages
            lstDisplay.Items.Clear();
            List<Packages> pkgList = new DbAccess().SelectPackages();

            foreach (Packages entry in pkgList)
                { lstDisplay.Items.Add(entry); }

        }

        private void FormPackages_Load(object sender, EventArgs e)
        {
            //Added by: Moses
            //Added on: 2012/01/20

            PopulateCmb();
            clearPkgInfoPane();
            clearPkgListPane();
        }

        private void cmbSearchSuppliers_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Added by: Moses
            //Added on: 2012/01/20

            List<ProductsSuppliers> searchResults = new Search().filterProSup(this);

            clbProdSups.Items.Clear();
            foreach (ProductsSuppliers entry in searchResults)
            {
                clbProdSups.Items.Add(entry, false);
                //to-do: put logic in to determine if we should check by default or not
            }
            
        }

        private void cmbSearchProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Added by: Moses
            //Added on: 2012/01/20

            List<ProductsSuppliers> searchResults = new Search().filterProSup(this);

            clbProdSups.Items.Clear();
            foreach (ProductsSuppliers entry in searchResults)
            {
                clbProdSups.Items.Add(entry, false);
                //to-do: put logic in to determine if we should check or not
            }
        }

    }
}